﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class GoodsAllot
    {
        public int Add(Chain.Model.GoodsAllot model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into GoodsAllot(");
            builder.Append("AllotAccount,AllotOutShopID,AllotInShopID,AllotCreateTime,AllotTotalNumber,AllotUserID,AllotRemark,AllotState)");
            builder.Append(" values (");
            builder.Append("@AllotAccount,@AllotOutShopID,@AllotInShopID,@AllotCreateTime,@AllotTotalNumber,@AllotUserID,@AllotRemark,@AllotState)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AllotAccount", SqlDbType.VarChar, 50), new SqlParameter("@AllotOutShopID", SqlDbType.Int, 4), new SqlParameter("@AllotInShopID", SqlDbType.Int, 4), new SqlParameter("@AllotCreateTime", SqlDbType.DateTime), new SqlParameter("@AllotTotalNumber", SqlDbType.Float, 8), new SqlParameter("@AllotUserID", SqlDbType.Int, 4), new SqlParameter("@AllotRemark", SqlDbType.NVarChar, 500), new SqlParameter("@AllotState", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.AllotAccount;
            cmdParms[1].Value = model.AllotOutShopID;
            cmdParms[2].Value = model.AllotInShopID;
            cmdParms[3].Value = model.AllotCreateTime;
            cmdParms[4].Value = model.AllotTotalNumber;
            cmdParms[5].Value = model.AllotUserID;
            cmdParms[6].Value = model.AllotRemark;
            cmdParms[7].Value = model.Allotstate;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int AllotID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GoodsAllot ");
            builder.Append(" where AllotID=@AllotID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AllotID", SqlDbType.Int, 4) };
            cmdParms[0].Value = AllotID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string AllotIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GoodsAllot ");
            builder.Append(" where AllotID in (" + AllotIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int AllotID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from GoodsAllot");
            builder.Append(" where AllotID=@AllotID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AllotID", SqlDbType.Int, 4) };
            cmdParms[0].Value = AllotID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select AllotID,AllotAccount,AllotOutShopID,AllotInShopID,AllotCreateTime,AllotTotalNumber,AllotUserID,AllotRemark,AllotState ");
            builder.Append(" FROM GoodsAllot ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" AllotID,AllotAccount,AllotOutShopID,AllotInShopID,AllotCreateTime,AllotTotalNumber,AllotUserID,AllotRemark,AllotState ");
            builder.Append(" FROM GoodsAllot ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.AllotID desc");
            }
            builder.Append(")AS Row, T.*  from GoodsAllot T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "GoodsAllot,SysShop,SysUser";
            string[] column = new string[] { "GoodsAllot.*,SysShop.ShopName,SysUser.UserName,(select ShopName from SysShop where GoodsAllot.AllotOutShopID=SysShop.ShopID) as AllotOutShopName,(select ShopName from SysShop where GoodsAllot.AllotInShopID=SysShop.ShopID) as AllotInShopName" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "AllotCreateTime", "AllotID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public Chain.Model.GoodsAllot GetModel(int AllotID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 AllotID,AllotAccount,AllotOutShopID,AllotInShopID,AllotCreateTime,AllotTotalNumber,AllotUserID,AllotRemark,AllotState from GoodsAllot ");
            builder.Append(" where AllotID=@AllotID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AllotID", SqlDbType.Int, 4) };
            cmdParms[0].Value = AllotID;
            Chain.Model.GoodsAllot allot = new Chain.Model.GoodsAllot();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["AllotID"] != null) && (set.Tables[0].Rows[0]["AllotID"].ToString() != ""))
            {
                allot.AllotID = int.Parse(set.Tables[0].Rows[0]["AllotID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["AllotAccount"] != null) && (set.Tables[0].Rows[0]["AllotAccount"].ToString() != ""))
            {
                allot.AllotAccount = set.Tables[0].Rows[0]["AllotAccount"].ToString();
            }
            if ((set.Tables[0].Rows[0]["AllotOutShopID"] != null) && (set.Tables[0].Rows[0]["AllotOutShopID"].ToString() != ""))
            {
                allot.AllotOutShopID = int.Parse(set.Tables[0].Rows[0]["AllotOutShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["AllotInShopID"] != null) && (set.Tables[0].Rows[0]["AllotInShopID"].ToString() != ""))
            {
                allot.AllotInShopID = int.Parse(set.Tables[0].Rows[0]["AllotInShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["AllotCreateTime"] != null) && (set.Tables[0].Rows[0]["AllotCreateTime"].ToString() != ""))
            {
                allot.AllotCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["AllotCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["AllotTotalNumber"] != null) && (set.Tables[0].Rows[0]["AllotTotalNumber"].ToString() != ""))
            {
                allot.AllotTotalNumber = decimal.Parse(set.Tables[0].Rows[0]["AllotTotalNumber"].ToString());
            }
            if ((set.Tables[0].Rows[0]["AllotUserID"] != null) && (set.Tables[0].Rows[0]["AllotUserID"].ToString() != ""))
            {
                allot.AllotUserID = int.Parse(set.Tables[0].Rows[0]["AllotUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["AllotRemark"] != null) && (set.Tables[0].Rows[0]["AllotRemark"].ToString() != ""))
            {
                allot.AllotRemark = set.Tables[0].Rows[0]["AllotRemark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["AllotState"] != null) && (set.Tables[0].Rows[0]["AllotState"].ToString() != ""))
            {
                allot.Allotstate = int.Parse(set.Tables[0].Rows[0]["AllotState"].ToString());
            }
            return allot;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM GoodsAllot ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.GoodsAllot model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update GoodsAllot set ");
            builder.Append("AllotAccount=@AllotAccount,");
            builder.Append("AllotOutShopID=@AllotOutShopID,");
            builder.Append("AllotInShopID=@AllotInShopID,");
            builder.Append("AllotCreateTime=@AllotCreateTime,");
            builder.Append("AllotTotalNumber=@AllotTotalNumber,");
            builder.Append("AllotUserID=@AllotUserID,");
            builder.Append("AllotRemark=@AllotRemark,");
            builder.Append("AllotState=@AllotState");
            builder.Append(" where AllotID=@AllotID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AllotAccount", SqlDbType.VarChar, 50), new SqlParameter("@AllotOutShopID", SqlDbType.Int, 4), new SqlParameter("@AllotInShopID", SqlDbType.Int, 4), new SqlParameter("@AllotCreateTime", SqlDbType.DateTime), new SqlParameter("@AllotTotalNumber", SqlDbType.Float, 8), new SqlParameter("@AllotUserID", SqlDbType.Int, 4), new SqlParameter("@AllotRemark", SqlDbType.NVarChar, 500), new SqlParameter("@AllotState", SqlDbType.Int, 4), new SqlParameter("@AllotID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.AllotAccount;
            cmdParms[1].Value = model.AllotOutShopID;
            cmdParms[2].Value = model.AllotInShopID;
            cmdParms[3].Value = model.AllotCreateTime;
            cmdParms[4].Value = model.AllotTotalNumber;
            cmdParms[5].Value = model.AllotUserID;
            cmdParms[6].Value = model.AllotRemark;
            cmdParms[7].Value = model.Allotstate;
            cmdParms[8].Value = model.AllotID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

